package com.oncallmng.api;

import com.oncallmng.common.Result;
import com.oncallmng.service.AlertService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * Webhook控制器
 */
@Slf4j
@RestController
@RequestMapping("/webhook")
@RequiredArgsConstructor
public class WebhookController {
    
    private final AlertService alertService;
    
    /**
     * 接收Prometheus告警
     * @param payload 告警数据
     * @return 结果
     */
    @PostMapping("/prometheus")
    public Result<String> prometheus(@RequestBody String payload) {
        log.info("接收到Prometheus告警: {}", payload);
        String alertId = alertService.receiveWebhook("PROMETHEUS", payload);
        return Result.success("接收成功", alertId);
    }
    
    /**
     * 接收Zabbix告警
     * @param payload 告警数据
     * @return 结果
     */
    @PostMapping("/zabbix")
    public Result<String> zabbix(@RequestBody String payload) {
        log.info("接收到Zabbix告警: {}", payload);
        String alertId = alertService.receiveWebhook("ZABBIX", payload);
        return Result.success("接收成功", alertId);
    }
    
    /**
     * 接收Grafana告警
     * @param payload 告警数据
     * @return 结果
     */
    @PostMapping("/grafana")
    public Result<String> grafana(@RequestBody String payload) {
        log.info("接收到Grafana告警: {}", payload);
        String alertId = alertService.receiveWebhook("GRAFANA", payload);
        return Result.success("接收成功", alertId);
    }
    
    /**
     * 接收自定义Webhook告警
     * @param token 令牌
     * @param payload 告警数据
     * @return 结果
     */
    @PostMapping("/custom/{token}")
    public Result<String> custom(@PathVariable String token, @RequestBody String payload) {
        log.info("接收到自定义Webhook告警，令牌: {}, 数据: {}", token, payload);
        // 这里可以根据token查询对应的告警源配置
        String alertId = alertService.receiveWebhook("CUSTOM_WEBHOOK", payload);
        return Result.success("接收成功", alertId);
    }
} 